package com.srs.action.scope;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.srs.action.BaseAction;
import com.srs.dao.RGroupScopeDao;
import com.srs.dao.cfg.AreaDao;
import com.srs.dao.cfg.CountryDao;
import com.srs.dao.cfg.ProvinceDao;
import com.srs.dao.cfg.ScopeDao;
import com.srs.model.cfg.Area;
import com.srs.model.cfg.Country;
import com.srs.model.cfg.Province;
import com.srs.model.cfg.Scope;
import com.srs.model.cfg.ScopeItem;

public class ScopeAction extends BaseAction {

	private Integer page;
	private Integer pageNum;	
	private Integer start;
	private Integer end;
	private Integer total;	
	
	private Scope scope;
	private List<Scope> scopes;

	private String scopesInGroup;
	
	private String json;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Action scope/list - Scope management
	 * 
	 * @return
	 */
	public String list() {
		System.out.println("ScopeAction::list");
		ScopeDao scopeDao = new ScopeDao();
		scopes = scopeDao.list();
		
		if (page==null){
			page = 1;
		}
		if (scopes.size()%RECORD_PER_PAGE==0){
			pageNum = scopes.size()/RECORD_PER_PAGE;
		} else {
			pageNum = scopes.size()/RECORD_PER_PAGE + 1;
		}
		
		start = (page-1)*RECORD_PER_PAGE + 1;
		total = scopes.size();
		if (total<(page)*RECORD_PER_PAGE){
			end = total;
		} else {
			end = (page)*RECORD_PER_PAGE;
		}
		
		List<Scope> pageScopes = new ArrayList<Scope>() ;
		for(int i=0; i<scopes.size(); i++){
			if (i>=(page-1)*RECORD_PER_PAGE && i<(page)*RECORD_PER_PAGE){
				scopes.get(i).setIndex(i+1);
				pageScopes.add(scopes.get(i));
			}
		}
		scopes = pageScopes;		
		return SUCCESS;
	}

	/**
	 * Action scope/view - Scope management
	 * 
	 * @return
	 */
	public String view() {
		System.out.println("ScopeAction::view");
		ScopeDao scopeDao = new ScopeDao();
		scope = scopeDao.get(scope.getScopeId());
		return SUCCESS;
	}

	/**
	 * Action scope/add - Scope management
	 * 
	 * @return
	 */
	public String add() {
		System.out.println("ScopeAction::add");
		return SUCCESS;
	}

	/**
	 * Action scope/addComplete - Scope management
	 * 
	 * @return
	 */
	public String addComplete() {
		System.out.println("ScopeAction::addComplete");
		ScopeDao scopeDao = new ScopeDao();
		scopeDao.insert(scope);		
		return SUCCESS;
	}

	/**
	 * Action scope/edit - Scope management
	 * 
	 * @return
	 */
	public String edit() {
		System.out.println("ScopeAction::edit");
		ScopeDao scopeDao = new ScopeDao();
		scope = scopeDao.get(scope.getScopeId());
		return SUCCESS;
	}

	/**
	 * Action scope/editComplete - Scope management
	 * 
	 * @return
	 */
	public String editComplete() {
		System.out.println("ScopeAction::editComplete");
		ScopeDao scopeDao = new ScopeDao();
		scopeDao.update(scope);
		return SUCCESS;
	}

	/**
	 * Action scope/deleteComplete - Scope management
	 * 
	 */
	public String deleteComplete() {
		System.out.println("ScopeAction::deleteComplete");
		ScopeDao scopeDao = new ScopeDao();
		scopeDao.delete(scope.getScopeId());
		return SUCCESS;
	}

	/**
	 * Action role/listJson - Role management
	 * 
	 */
	public String listJson() {
		System.out.println("ScopeAction::listJson");

		// Init Dao
		ScopeDao scopeDao = new ScopeDao();
		
		
		// Put to json;
		setJson("{scopes: " +
				JSONArray.fromObject(scopeDao.list(scopesInGroup)).toString() +
				"}");
		return SUCCESS;
	}		
	
	/**
	 * Action role/listJson - Role management
	 * 
	 */
	public String listItemJson() {
		System.out.println("ScopeAction::listItemJson");

		// Init Dao
		ScopeDao scopeDao = new ScopeDao();
		scope = scopeDao.get(scope.getScopeId());
		List<ScopeItem> scopeItems = new ArrayList<ScopeItem>();
		if (scope.getScopeName().equalsIgnoreCase("SCOPE_COUNTRY")){
			CountryDao countryDao = new CountryDao();
			List<Country> countries = countryDao.list();
			for (Country country : countries) {
				ScopeItem scopeItem = new ScopeItem();
				scopeItem.setId(country.getCountryId());
				scopeItem.setName(country.getCountryName());
				scopeItems.add(scopeItem);
			}
		} else if (scope.getScopeName().equalsIgnoreCase("SCOPE_AREA")){
			AreaDao areaDao = new AreaDao();
			List<Area> areas = areaDao.list();
			for (Area area : areas) {
				ScopeItem scopeItem = new ScopeItem();
				scopeItem.setId(area.getAreaId());
				scopeItem.setName(area.getAreaName());
				scopeItems.add(scopeItem);				
			}
		} else if (scope.getScopeName().equalsIgnoreCase("SCOPE_PROVINCE")){
			ProvinceDao provinceDao = new ProvinceDao();
			List<Province> provinces = provinceDao.list();
			for (Province province : provinces) {
				ScopeItem scopeItem = new ScopeItem();
				scopeItem.setId(province.getProvinceId());
				scopeItem.setName(province.getProvinceName());
				scopeItems.add(scopeItem);				
			}
		}
		// Put to json;
		setJson("{scopeItems: " +
				JSONArray.fromObject(scopeItems).toString() +
				"}");
		return SUCCESS;
	}		
	
	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String nameIsUsed() {
		System.out.println("ScopeAction::listJson");

		// Init Dao
		ScopeDao scopeDao = new ScopeDao();
		
		// Put to json;
		setJson("{nameIsUsed: " +
				JSONArray.fromObject(scopeDao.nameIsUsed(scope.getScopeName())).toString() +
				"}");
		return SUCCESS;
	}	
	

	public String scopeIsUsed() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		RGroupScopeDao groupScopeDao = new RGroupScopeDao();
		
		// Put to json;
		setJson("{scopeIsUsed: " +
				JSONArray.fromObject(groupScopeDao.scopeIsUsed(scope.getScopeId())).toString() +
				"}");
		return SUCCESS;		
	}
		
	
	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}

	/**
	 * @param pageNum the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * @return the start
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * @return the end
	 */
	public Integer getEnd() {
		return end;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(Integer end) {
		this.end = end;
	}

	/**
	 * @return the total
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return the scope
	 */
	public Scope getScope() {
		return scope;
	}

	/**
	 * @param scope the scope to set
	 */
	public void setScope(Scope scope) {
		this.scope = scope;
	}

	/**
	 * @return the scopes
	 */
	public List<Scope> getScopes() {
		return scopes;
	}

	/**
	 * @param scopes the scopes to set
	 */
	public void setScopes(List<Scope> scopes) {
		this.scopes = scopes;
	}

	/**
	 * @return the scopesInGroup
	 */
	public String getScopesInGroup() {
		return scopesInGroup;
	}

	/**
	 * @param scopesInGroup the scopesInGroup to set
	 */
	public void setScopesInGroup(String scopesInGroup) {
		this.scopesInGroup = scopesInGroup;
	}

	/**
	 * @return the json
	 */
	public String getJson() {
		return json;
	}

	/**
	 * @param json the json to set
	 */
	public void setJson(String json) {
		this.json = json;
	}
	
}
